/**
 *  File: SendSmsService.java
 *  Date         Author      Changes
 *  2016-10-2    wangtq       Create
 *
 */
package com.kunpu.mami.channel.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.kunpu.mami.channel.common.ResConstants;
import com.kunpu.mami.channel.exception.MamiiException;

/**
 * 宝付发送短信接口服务
 * @author wangtq
 * @date 2016-10-2
 * @version 0.1
 *
 */
@Service
public class SendSmsService extends BaseSendService{
	
	private final Logger  logger = LoggerFactory.getLogger(this.getClass());
	
	/**
	    * 发送短信
	    * @param transId   交易流水号
	    * @param mobile    手机号
	    * @param accNo     银行卡号
	    * @param txnAmt    交易金额
	    * @param bindId    银行返回bindId
	    * @param nextTxnSubType  绑卡[01]/支付[04]
	    * @return
	    */
	public String sendSms(Map<String,Object> sendSmsParam){
		logger.info("=============================== 渠道service sendSMS start ================,\n入参{}:",sendSmsParam);
		try{
			//1:参数验证
			validateParam(sendSmsParam);
			//2:组装参数
			String sendContent = this.buildSendSmsParm(sendSmsParam);
			//3:发送报文
			String txn_sub_type = "05";// 交易子类
			this.send(sendContent,"sendSms",txn_sub_type);
		}catch(IllegalArgumentException ill){
			logger.error("==================== sendSMS 发送短信时参数校验异常，异常信息为：\n",ill.getMessage(),ill);
			throw new MamiiException(ResConstants.PARAM_NOT_NULL.getCode() , ill.getMessage());
		}catch(MamiiException me){
			logger.error("==================== sendSMS 发送短信时异常，异常信息为：\n",me.getMessage(),me);
			throw new MamiiException(me.getErrorCode() , me.getMessage());
		}catch(Exception e){
			logger.error("==================== sendSMS 发送短信时异常，异常信息为：\n",e.getMessage(),e);
			throw new MamiiException(ResConstants.SMS_SEND_EXCEPTION.getCode() , e.getMessage());
		}
		
		logger.info("===================== 渠道service sendSMS end ====================");
		return null;
	}
	
	
		/**
		 * 获取验证码组装报文
		 * 
		 * @param trans_id
		 *            唯一订单号，8-20
		 *            位字母和数字，同一天内不可重复；如果商户开通“发送短信类交易”，该订单号从发送短信类交易到当前交易都有效
		 * @param acc_no
		 *            绑定的卡号
		 * @param mobile
		 *            银行卡绑定手机号
		 * @param bind_id
		 *            绑定标识号
		 * @param txn_amt
		 *            交易金额 单位：例：1元则提交为100
		 * @param next_txn_sub_type
		 *            下一步进行的交易子类
		 * @param additional_info
		 *            附加字段
		 * @param req_reserved
		 *            请求方保留域 长度不超过 128 位
		 */
		private String buildSendSmsParm(Map<String,Object> sendSmsParam) {
			String trade_date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
			String txn_sub_type = "05";// 交易子类
			String additionalInfo = "附加字段";
			String reqReserved = "请求方保留域";
			StringBuilder sb = new StringBuilder();
			sb.append("{");
			sb.append("\"txn_sub_type\":\"" + txn_sub_type + "\"");//
			sb.append(",\"biz_type\":\"0000\"");// 接入类型默认0000
			sb.append(",\"terminal_id\":\"" + terminal_id + "\"");
			sb.append(",\"member_id\":\"" + member_id + "\"");
			sb.append(",\"trans_serial_no\":\"" + member_id + "" + trade_date + "\"");
			sb.append(",\"trans_id\":\"" + sendSmsParam.get("trans_id") + "\"");
			sb.append(",\"acc_no\":\"" + sendSmsParam.get("acc_no")  + "\"");
			sb.append(",\"mobile\":\"" + sendSmsParam.get("mobile") + "\"");
			sb.append(",\"bind_id\":\"" + sendSmsParam.get("bind_id")  + "\"");
			sb.append(",\"txn_amt\":\"" + sendSmsParam.get("txn_amt") + "\"");
			sb.append(",\"next_txn_sub_type\":\"" + sendSmsParam.get("next_txn_sub_type") + "\"");
			sb.append(",\"trade_date\":\"" + trade_date + "\"");
			sb.append(",\"additional_info\":\"" + additionalInfo + "\"");
			sb.append(",\"req_reserved\":\"" + reqReserved + "\"");
			sb.append("}");
		
			return sb.toString();

	}
		
   /**
    * 验证入参
    */		
	private void validateParam(Map<String,Object>param)throws IllegalArgumentException{
		Assert.isTrue(!CollectionUtils.isEmpty(param),"入参为空");
		Assert.isTrue(!StringUtils.isEmpty((String)param.get("mobile")),"手机号不能为空");
	}
	

	
}
